package server.business.http.authority;

import server.business.entity.Response;
import server.business.entity.User;

import com.alibaba.fastjson.JSON;
import com.core.rest.RequestMethod;
import com.core.rest.RestClient;


public class AuthorityServiceImpl implements IAuthorityService{

    
    public boolean isValide(String tokenId,String ipAddr,int port) {
        
        RestClient rc = new RestClient("http://"+ipAddr+":"+port+"/tuhaopt/authority/verify");
        
        rc.addParam("tokenId", tokenId);
        
        try {
            
            rc.execute(RequestMethod.POST);
            
            String response = rc.getResponse();
            
            Response rs = JSON.parseObject(response, Response.class);
            
            if(rs.getResponseCode() != 1){
                
                return false;
            }
            
            return true;
            
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        
        return false;
    }

    
    public boolean register(User user,String ipAddr,int port) {
        RestClient rc = new RestClient("http://"+ipAddr+":"+port+"/tuhaopt/authority/save");
        
        rc.addParam("name", user.getName());
        
        rc.addParam("password", user.getPassword());
        
        rc.addParam("email", user.getEmail());
        
        try {
            
            rc.execute(RequestMethod.POST);
            
            String response = rc.getResponse();
            //System.out.println(response);
            Response rs = JSON.parseObject(response, Response.class);
            
            if(rs.getResponseCode() != 1){
                
                System.out.println(rs.getMessage());
                
                return false;
            }
            
            return true;
            
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        
        return false;
    }
    
    public static void main(String[]arg){
        AuthorityServiceImpl asi = new AuthorityServiceImpl();
        
        User u = new User();
        u.setName("jiongjiong1");
        u.setPassword("macky");
        u.setEmail("doctormacky@cndd.ibm.com");
        
        asi.register(u, "127.0.0.1", 9090);
    }

}
